Repository navigation
feat(dialect): add session transaction control - #409
Conversation
061b159 to
8d34bae
Compare
3d5bf40 to
2f4e0b7
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #409 +/- ##
==========================================
- Coverage 93.08% 92.98% -0.10%
==========================================
Files 282 283 +1
Lines 39885 40118 +233
==========================================
+ Hits 37125 37304 +179
- Misses 2760 2814 +54
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
2f4e0b7 to
c0b35f9
Compare
|
Rebased onto current main (after #407). Still a single commit, no conflicts. |
Add StartTransaction, Commit, Rollback and SetAutocommit statements to the KiteSQL AST. The generic parser lowers BEGIN or START TRANSACTION, COMMIT and ROLLBACK, rejecting transaction modes, savepoints and AND CHAIN, and lowers SET autocommit = 0 | 1 (or ON/OFF, TRUE/FALSE) only after SqlParser::with_set_autocommit(); otherwise SET autocommit is rejected like any other SET. The SQLite parser lowers BEGIN [DEFERRED], COMMIT/END and ROLLBACK. Add Database::session(), which returns a Session that runs these statements. Between BEGIN and COMMIT or ROLLBACK, statements run in one transaction, as with Transaction::run; outside one, each statement commits on its own, as with Database::run. COMMIT and ROLLBACK without an open transaction do nothing, and dropping the session rolls back an open transaction. With autocommit off, a statement outside a transaction opens one that stays open until COMMIT or ROLLBACK, and turning autocommit back on commits it. DDL and ANALYZE still go through Database::ddl and Database::analyze, and the other entry points reject transaction control statements. Add SqlDialect::begin_commits_active_transaction(), defaulting to false: when true, BEGIN inside an open transaction commits it and opens a new one; by default such a BEGIN is rejected and the open transaction is left as it was. DynDialect forwards it to its current dialect. Database::run and Transaction::run now delegate to run_statements, which the session reuses for statements it has already parsed. Their behaviour is unchanged. Unit tests in src/db/session.rs cover the default and a test-only dialect that parses SET autocommit and turns the hook on. Co-authored-by: Alex Skinner <alexskinner@users.noreply.github.com>
c0b35f9 to
8a8bac6
Compare
There was a problem hiding this comment.
Apologies for the late reply. This is an issue we should perhaps prioritize for discussion. I previously implemented a server for KiteSQL that supports the pgwire protocol, and I encountered similar challenges regarding the parsing of SQL transaction statements. KiteSQL itself does not natively support standard transaction commands like START TRANSACTION or COMMIT, as I designed the database to be used primarily through native code-based interactions—where you can handle transactions (e.g., BEGIN, ROLLBACK, COMMIT) directly within your application code. I believe this PR is valuable; it introduces a useful abstraction for parsing such statements. Thank you for your design and contribution!
Part of #403.
Reworked on top of #410: the transaction control statements are now part of the KiteSQL AST, and the dialect's parser decides which of them it accepts.
What
Statement::StartTransaction,Commit,RollbackandSetAutocommit(bool).BEGIN/START TRANSACTION,COMMITandROLLBACK, and rejects transaction modes, savepoints andAND CHAIN.SET autocommit = 0 | 1(andON/OFF,TRUE/FALSE) is lowered only afterSqlParser::with_set_autocommit(). Otherwise it is rejected like any otherSET, as today.BEGIN [DEFERRED],COMMIT/ENDandROLLBACK, soSessionworks withSqliteDialecttoo. Named transactions,IMMEDIATE/EXCLUSIVEand savepoints stay rejected.Database::session(), which returns aSessionthat runs those statements:BEGINandCOMMIT/ROLLBACK, statements run in one transaction, as withTransaction::run. Outside a transaction, each statement commits on its own, as withDatabase::run.COMMITandROLLBACKwithout an open transaction do nothing. Dropping the session rolls back an open transaction.COMMITorROLLBACK, and turning autocommit back on commits it.ANALYZEstill go throughDatabase::ddlandDatabase::analyze, with the same errors as today.Database::run,Transaction::runand the other entry points reject transaction control statements and point toDatabase::session.Session::runtakes one statement at a time.autocommit()andin_transaction()report the session's state.SqlDialecthook,begin_commits_active_transaction(), defaulting tofalse. Whentrue,BEGINinside an open transaction commits it and opens a new one. By default such aBEGINis rejected and the open transaction is left as it was.DynDialectforwards it.Database::runandTransaction::runnow delegate to a crate-privaterun_statements, which the session reuses for the statement it has already parsed. Neither public method changes behaviour.Session, the parser option and the hook, a line in the MVCC section and a line in the dialect section ofdocs/features.md.Why
Drivers and connection pools send transaction control as SQL. Today that only works through the CLI shell, which special-cases those strings. A
Sessiongives embedders the same thing through the library. Dialects also differ on the details: MySQL-compatible clients expect a nestedBEGINto commit implicitly and useSET autocommit = 0, while the standard behaviour is to reject both. With #410 a dialect crate opts intoSET autocommitin its ownparseand picks the nestedBEGINbehaviour through the hook, without branches in core. See #403.Testing
src/db/session.rs, on the default test storage for both the sync and async builds: with the generic dialect a nestedBEGINandSET autocommitare rejected and the open transaction still commits; with a test-only dialect whoseparsecallsSqlParser::with_set_autocommit(), a nestedBEGINcommits, and autocommit off opens a transaction that turning it back on commits.test_prepare_and_command_type_classificationcase forstart transactionnow checks thatcommand_typerejects it with a pointer toDatabase::session, since the parser accepts it.make test(657 passed, 2 ignored),make test-async(602 passed),make test-sltandmake test-slt-async(198 files each),make fmtand all fourmake clippyinvocations pass.Notes for review
src/dialect/mod.rs,src/parser/genericanddocs/features.mdas that PR, so whichever lands second needs a trivial rebase.SET autocommitgate lives in the parser rather than in aSqlDialecthook, following feat: KiteSQL AST and SQLite dialect #410's "the dialect rejects syntax it does not support" rule. Only the nestedBEGINbehaviour, which is semantics, stays a hook.Sessionis additive. Nothing that exists today goes through it, and with the default dialect the only new behaviour is the session itself.Transaction::rundoes today.ROLLBACKdiscards it. Statement-level rollback inside a transaction would need savepoint support in the storages, so I left it out of scope.Databaseon the same thread while a session holds a write transaction waits for that transaction. The rustdoc says to send every statement through the session while it has one open.src/bin/shell.rsover toSession, to keep this PR to one concern. I'm happy to do that in a follow-up if you'd like.